<?php

namespace app\api\controller;

use app\api\model\wanlshop\Address;
use app\common\controller\Api;
use think\Db;

/**
 * 设置小区.社区区域
 */
class AddressLocation extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    
    //社区,小区购买记录
    public function getorder(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $gid = $this->request->request('gid');
        $free = $this->request->request('free');
        $uid = $this->auth->id;
        $wherenew = "(user_id = $uid  AND status = 'paid'  AND gid = $gid) OR (dk_uid= $uid  AND status = 'paid'  AND gid = $gid)";
        $list = model('app\common\model\GradeOrder')
            ->where($wherenew)
            ->field('id,gid,updatetime,is_dk,dk_uid')
            ->order('updatetime desc')
            ->paginate()->each(function ($item,$key){
                $item['child'] = model('\app\common\model\AddressLocation')
//                    ->where(array('uid'=>$this->auth->id))
                    ->where(array('oid'=>$item['id']))
                    ->select();
                return $item;
            }
            );
        $list?($this->success('ok',$list)):($this->error(__('网络繁忙')));
    }

    //该订单有没有设置过社区
    public function checkmanage(){
        if ($this->request->isPost()) {
            //设置过滤方法
            $this->request->filter(['strip_tags']);
            $data = $this->request->post();
            //查询是否有社区购买记录条数
           /*$ordercount = model('app\common\model\GradeOrder')
                ->where(['status' => 'paid','gid'=>4])
                ->where('user_id',$this->auth->id)
                ->count();*/
            //已经设置社区数量
            $addressLocation = new \app\common\model\AddressLocation();
            /*$locationcount = $addressLocation->where(array('oid'=>$data['oid']))->where(array('uid'=>$this->auth->id))->count();//订单设置数量
            if($locationcount==1){
                $this->error('这条订单记录,已设置社区');
            }*/
            $item = $addressLocation->where(array('town_id'=>$data['town_id'],'type'=>'manage'))->find();
            if(!empty($item)){
                $this->error('该社区已有社区经理,无法设置');
            }
            $this->success('可以添加');
        }
    }

    //添加社区
    public function setmanage()
    {
        if ($this->request->isPost()) {
            //设置过滤方法
            $this->request->filter(['strip_tags']);
            $data = $this->request->post();
            //查询是否有社区购买记录条数
            /*$ordercount = model('app\common\model\GradeOrder')
                ->where(['status' => 'paid','gid'=>4])
                ->where('user_id',$this->auth->id)
                ->count();*/
            //已经设置社区数量
            $addressLocation = new \app\common\model\AddressLocation();
            /*$locationcount = $addressLocation->where(array('oid'=>$data['oid']))->where(array('uid'=>$this->auth->id))->count();//订单设置数量
            if($locationcount==1){
                $this->error('这条订单记录,已设置社区');
            }*/
            //查询该社区是否已经占用
            $item = $addressLocation->where(array('town_id'=>$data['town_id'],'type'=>'manage'))->find();
            if(!empty($item)){
                $this->error('该社区已有社区经理,无法设置');
            }
            //查询订单信息
            $GradeOrder = new \app\common\model\GradeOrder();
            $order = $GradeOrder->where('id',$data['oid'])->find();
            if($order['is_dk']==1){
                $data['uid'] = $order['dk_uid'];
            }else{
                $data['uid'] = $this->auth->id;
            }
            $data['type'] = 'manage';
            $addressLocation->allowField(true)->save($data);
            $this->success('设置成功');
        }
    }

    public function checksubdstrict(){
        if ($this->request->isPost()) {
            //设置过滤方法
            $this->request->filter(['strip_tags']);
            $data = $this->request->post();
            //查询是否有社区购买记录条数
            /*$ordercount = model('app\common\model\GradeOrder')
                ->where(['status' => 'paid','gid'=>3])
                ->where('user_id',$this->auth->id)
                ->count();*/
            //已经设置小区数量
            $addressLocation = new \app\common\model\AddressLocation();
            /*if($data['free'] == 0){
                $locationcount = $addressLocation
                    ->where(array('oid'=>$data['oid']))->count();
                if($locationcount>=2){
                    $this->error('这条订单,没有未设置的小区');
                }
            }*/
            //查询该小区是否已经占用
            $subdstrictitem = Db::name('area_subdstrict')
                ->where(array(
                    'province_id'=>$data['province_id'],
                    'city_id'=>$data['city_id'],
                    'region_id'=>$data['region_id'],
                    'name'=>$data['detail']
                ))->find();
            if(!empty($subdstrictitem)){
                if($data['free']==1){//免费申请检测
                    $item = $addressLocation->where(array('subdstrict_id'=>$subdstrictitem['id']))->find();
                }else{
                    //付费检测已付费的
                    $item = $addressLocation->where(array('subdstrict_id'=>$subdstrictitem['id']))->where('free','neq',1)->find();
                }
                if(!empty($item)){
                    $this->error('该小区已有小区管理员,无法设置');
                }
            }
            $this->success('可以添加');
        }
    }

    //添加小区
    public function setsubdstrict()
    {
        if ($this->request->isPost()) {
            //设置过滤方法
            $this->request->filter(['strip_tags']);
            $data = $this->request->post();
            //查询是否有小区购买记录条数
            /*$ordercount = model('app\common\model\GradeOrder')
                ->where(['status' => 'paid','gid'=>3])
                ->where('user_id',$this->auth->id)
                ->count();*/
            //已经设置小区数量
            $addressLocation = new \app\common\model\AddressLocation();
            /*if($data['free'] == 0){
                $locationcount = $addressLocation
                    ->where(array('oid'=>$data['oid']))->count();
                if($locationcount>=2){
                    $this->error('这条订单,没有未设置的小区');
                }
            }*/
            //查询该小区是否已经占用
            $subdstrictitem = Db::name('area_subdstrict')
                ->where(array(
                    'province_id'=>$data['province_id'],
                    'city_id'=>$data['city_id'],
                    'region_id'=>$data['region_id'],
                    'name'=>$data['detail']
                ))->find();
            if(!empty($subdstrictitem)){
                if($data['free']==1){//免费申请检测
                    $item = $addressLocation->where(array('subdstrict_id'=>$subdstrictitem['id']))->find();
                }else{
                    //付费检测已付费的
                    $item = $addressLocation->where(array('subdstrict_id'=>$subdstrictitem['id']))->where('free','neq',1)->find();
                }
                if(!empty($item)){
                    $this->error('该小区已有小区管理员,无法设置');
                }
                $data['subdstrict_id'] = $subdstrictitem['id'];
            }else{
                //插入
                $subdstrict = [
                    'province_id'=>$data['province_id'],
                    'city_id'=>$data['city_id'],
                    'region_id'=>$data['region_id'],
                    'town_id2'=>$data['town_id'],
                    'name'=>$data['detail'],
                ];
                $data['subdstrict_id'] = Db::name('area_subdstrict')->insertGetId($subdstrict);
                //免费的修改用户等级
                /*if($data['free']==1){//免费申请检测
                    //修改等级
                    $user = model('app\common\model\User')
                        ->where(['id' => $this->auth->id])
                        ->find();
                    if($user['level']<3){
                        $user->level = 3;
                        $user->save();
                    }
                }*/
            }
            //查询订单信息
            $GradeOrder = new \app\common\model\GradeOrder();
            $order = $GradeOrder->where('id',$data['oid'])->find();
            if($order['is_dk']==1){
                $data['uid'] = $order['dk_uid'];
            }else{
                $data['uid'] = $this->auth->id;
            }
            $data['type'] = 'admin';
            $addressLocation->allowField(true)->save($data);
            $this->success('设置成功');
        }
    }

    //获取小区记录
    public function getfreesubdstrict()
    {
        if ($this->request->isPost()) {
            //设置过滤方法
            $this->request->filter(['strip_tags']);
            $where = [
                'uid'=>['eq',$this->auth->id],
                'free'=>['eq',1],
            ];
            $list = model('\app\common\model\AddressLocation')
                ->where($where)
                ->where('type','admin')
                ->select();
            $list?($this->success('ok',$list)):($this->error(__('未找到数据')));
        }
    }

    //我的区域列表
    public function list(){
        $gid = $this->request->request('gid');
        if($gid==3){
            $where['type'] = 'admin';
        }elseif ($gid==4){
            $where['type'] = 'manage';
        }
        $list = model('\app\common\model\AddressLocation')
            ->where(array('uid'=>$this->auth->id))
            ->where($where)
            ->select();
        $this->success('ok',$list);
    }

    //区域修改
    public function updatemanage()
    {
        if ($this->request->isPost()) {
            //设置过滤方法
            $this->request->filter(['strip_tags']);
            $data = $this->request->post();
            //已经设置社区数量
            $addressLocation = new \app\common\model\AddressLocation();
            //查询该社区是否已经占用
            $item = $addressLocation->where(array('town_id'=>$data['town_id'],'type'=>'manage'))->where('id','neq',$data['id'])->find();
            if(!empty($item)){
                $this->error('该社区已有社区经理,无法设置');
            }
            $data['uid'] = $this->auth->id;
            $data['type'] = 'manage';
            /*dump($data);die;*/
            $addressLocation->allowField(true)->save($data,array('id'=>$data['id']));
            $this->success('设置成功');
        }
    }

    //小区管理区域修改
    public function updatesubdstrict()
    {
        if ($this->request->isPost()) {
            //设置过滤方法
            $this->request->filter(['strip_tags']);
            $data = $this->request->post();
            //查询是否有小区购买记录条数
            //已经设置小区数量
            $addressLocation = new \app\common\model\AddressLocation();
            //不是您的记录不可修改
            /*$finditem = $addressLocation->where('id',$data['id'])->where('uid',$this->auth->id)->find();
            if(empty($finditem)){
                $this->error('此条记录不是您的,不可修改');
            }*/
            //查询该小区是否已经占用
            $subdstrictitem = Db::name('area_subdstrict')
                ->where(array(
                    'province_id'=>$data['province_id'],
                    'city_id'=>$data['city_id'],
                    'region_id'=>$data['region_id'],
                    'name'=>$data['detail']
                ))->find();
            if(!empty($subdstrictitem)){
                if($data['free']==1){//免费申请检测
                    $item = $addressLocation->where(array('subdstrict_id'=>$subdstrictitem['id']))->where('id','neq',$data['id'])->find();
                }else{
                    //付费检测已付费的
                    $item = $addressLocation->where(array('subdstrict_id'=>$subdstrictitem['id'],'free'=>0))->where('id','neq',$data['id'])->find();
                }
                if(!empty($item)){
                    $this->error('该小区已有小区管理员,无法设置');
                }
                $data['subdstrict_id'] = $subdstrictitem['id'];
            }else{
                //插入
                $subdstrict = [
                    'province_id'=>$data['province_id'],
                    'city_id'=>$data['city_id'],
                    'region_id'=>$data['region_id'],
                    'town_id2'=>$data['town_id'],
                    'name'=>$data['detail'],
                ];
                $data['subdstrict_id'] = Db::name('area_subdstrict')->insertGetId($subdstrict);
            }
            $data['uid'] = $this->auth->id;
            $data['type'] = 'admin';
            $addressLocation->allowField(true)->save($data,array('id'=>$data['id']));
            $this->success('设置成功');
        }
    }
    
    //查询我团队的用户
    public function inviteuser(){
        //一共多少个,已经设置多少个
        $data = Db::name('user')->where(array('id'=>$this->auth->id))->field('xiaoqucishu,xiaoqucishu_yiyong')->find();
        $data['team'] = model('\app\common\model\Team')
            ->alias('t')
            ->join('user u','t.get_user_id=u.id','LEFT')
            ->field('t.*,u.nickname,u.level')
            ->where('user_id',$this->auth->id)
            ->paginate(50)->each(function ($item,$key){
                //查询该用户是否被该社区经理添加过小区管理员
                $invitecount = Db::name('address_location')->where('type','admin')
                    ->where('inviteuid',$this->auth->id)
                    ->where('uid',$item['get_user_id'])
                    ->count();
                $item['flag'] = 0;
                if($invitecount>0){
                    $item['flag'] = 1;
                }
                return $item;
            });
        $this->success('ok',$data);
    }

    //社区经理添加小区管理员
    public function invitesubdstrict(){
        if ($this->request->isPost()) {
            //设置过滤方法
            $this->request->filter(['strip_tags']);
            $data = $this->request->post();
            //查询可添加的小区管理员数量
            $userItem = Db::name('user')->where('id',$this->auth->id)->find();
            if($userItem['xiaoqucishu']==0){
                $this->error('  添加小区管理员次数已用完');
            }
            if(empty($data['uid'])){
                $this->error('未传入小区管理员ID');
            }
            //已经设置小区数量
            $addressLocation = new \app\common\model\AddressLocation();
            //查询该小区是否已经占用
            $subdstrictitem = Db::name('area_subdstrict')
                ->where(array(
                    'province_id'=>$data['province_id'],
                    'city_id'=>$data['city_id'],
                    'region_id'=>$data['region_id'],
                    'name'=>$data['detail']
                ))->find();
            if(!empty($subdstrictitem)){
                //付费检测已付费的
                $item = $addressLocation->where(array('subdstrict_id'=>$subdstrictitem['id']))->where('free','neq',1)->find();
                if(!empty($item)){
                    $this->error('该小区已有小区管理员,无法设置');
                }
                $data['subdstrict_id'] = $subdstrictitem['id'];
            }else{
                //插入
                $subdstrict = [
                    'province_id'=>$data['province_id'],
                    'city_id'=>$data['city_id'],
                    'region_id'=>$data['region_id'],
                    'town_id2'=>$data['town_id'],
                    'name'=>$data['detail'],
                ];
                $data['subdstrict_id'] = Db::name('area_subdstrict')->insertGetId($subdstrict);
            }
            $data['type'] = 'admin';
            $data['free'] = 2;
            $data['inviteuid'] = $this->auth->id;
            $addressLocation = new \app\common\model\AddressLocation();
            $addressLocation->allowField(true)->save($data);
            Db::name('user')->where('id',$this->auth->id)
                ->update(array('xiaoqucishu'=>$userItem['xiaoqucishu']-1,'xiaoqucishu_yiyong'=>$userItem['xiaoqucishu_yiyong']+1));
            $this->success('设置成功');
        }
    }

}
